
import principalcontrole.principal;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import utilitario.Arquivo;
import utilitario.Funcionario;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author rodrig0
 */
public class login extends javax.swing.JFrame {

    public ArrayList<Funcionario> listaDeFuncionarios = new ArrayList<>();
    Funcionario admin = new Funcionario();

    /**
     * Creates new form login
     */
    public login() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLogin = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        txtLogin = new javax.swing.JTextField();
        btnOk = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        txtSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 225));
        setName("login"); // NOI18N
        setPreferredSize(new java.awt.Dimension(400, 225));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(null);

        lblLogin.setText("Login:");
        getContentPane().add(lblLogin);
        lblLogin.setBounds(67, 38, 40, 14);

        lblSenha.setText("Senha:");
        getContentPane().add(lblSenha);
        lblSenha.setBounds(60, 80, 40, 14);

        txtLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLoginKeyPressed(evt);
            }
        });
        getContentPane().add(txtLogin);
        txtLogin.setBounds(106, 31, 200, 28);

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
        getContentPane().add(btnOk);
        btnOk.setBounds(128, 125, 73, 23);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(219, 125, 86, 23);

        txtSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtSenhaKeyPressed(evt);
            }
        });
        getContentPane().add(txtSenha);
        txtSenha.setBounds(106, 70, 200, 28);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        listaDeFuncionarios = Arquivo.lerFuncionario();
        admin = Arquivo.lerAdmin();
    }//GEN-LAST:event_formWindowOpened

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        int res = -1;
        Funcionario logado;

        res = verificarConta();

        if (res == -2) {
            logado = admin;
            principal janela = new principal(logado);
            janela.setVisible(true);
            dispose();
        }

        if (res > -1) {
            logado = listaDeFuncionarios.get(res);
            principal janela = new principal(logado);
            janela.setVisible(true);
            dispose();
        }
        txtLogin.requestFocus();
        txtLogin.selectAll();
    }//GEN-LAST:event_btnOkActionPerformed

    private void txtLoginKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLoginKeyPressed
        if (evt.getKeyCode() == 10) {
            this.btnOkActionPerformed(null);
        }
    }//GEN-LAST:event_txtLoginKeyPressed

    private void txtSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSenhaKeyPressed
        if (evt.getKeyCode() == 10) {
            this.btnOkActionPerformed(null);
        }
    }//GEN-LAST:event_txtSenhaKeyPressed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnCancelarActionPerformed

    //metodo verifica se o login e senha conferem
    public int verificarConta() {
        int numero_funcionario = -1;

        //verificando se campos estao vazios
        if ((txtLogin.getText().isEmpty()) || (txtSenha.getText().isEmpty())) {
            JOptionPane.showMessageDialog(rootPane, "Login ou Senha esta vazio");
            return numero_funcionario;
        }

        //verificando se os campos informados correspondem ao administrador
        if (admin.getLogin().equalsIgnoreCase(txtLogin.getText())) {
            if (admin.getSenha().equalsIgnoreCase(txtSenha.getText())) {
                numero_funcionario = -2;
                return numero_funcionario;
            }
        }


        //verificando se o login e senha conferem com registro
        for (int i = 0; i < listaDeFuncionarios.size(); i++) {
            if (listaDeFuncionarios.get(i).getLogin().equalsIgnoreCase(txtLogin.getText())) {
                if (listaDeFuncionarios.get(i).getSenha().equalsIgnoreCase(txtSenha.getText())) {
                    numero_funcionario = i;
                    break;
                }
            }
        }

        if (numero_funcionario == -1) {
            JOptionPane.showConfirmDialog(rootPane, "Login ou senha incorreto(s)");
        }

        return numero_funcionario;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnOk;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JPasswordField txtSenha;
    // End of variables declaration//GEN-END:variables
}
